//
//  ViewController.m
//  UITableView3-7
//
//  Created by yb on 16/3/7.
//  Copyright © 2016年 yb. All rights reserved.
//

#import "ViewController.h"
#import "CZHero.h"

@interface ViewController ()<UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *tableView;

//数据模型的建立
@property (strong,nonatomic)NSArray *heroGroup;


@end

@implementation ViewController

//懒加载   延迟加载
//重写getter方法
- (NSArray *)heroGroup
{
    if (_heroGroup ==nil) {
        //读取Plist文件
        //读取plist文件
        //1.全路径
        //NSString *plistPath = [[NSBundle mainBundle]pathForResource:@"HeroPlist.plist" ofType:nil ];
         NSString *plistPath = [[NSBundle mainBundle]pathForResource:@"HeroPlist" ofType:@"plist"];
        //2.根据获取Plist文件的数据
        //需要将获取的数据保存到某个对象中，该对象类型和Plist文件的根节点的类型一致
        NSArray *array = [[NSArray alloc]initWithContentsOfFile:plistPath];
        NSLog(@"aaaaa===%@",array);
        //3.数组转模型，字典转模型
        //3.1面向模型开发，建立一个模型对象
        //3.2将Plist文件中key值作为模型的属性
        NSMutableArray *tempArray = [NSMutableArray array];
        for (NSDictionary *dict in array) {
            CZHero *heroGroup = [CZHero heroGroupWithDict:dict];
            [tempArray addObject:heroGroup];
        }
        _heroGroup = tempArray;
        
    }
    return _heroGroup;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    //设置数据源
    self.tableView.dataSource = self;
    
    
    
}
//返回tableView的组数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    //return 2;
    return self.heroGroup.count;
}
//返回每一组的行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
//    if (section == 0) {
//        return 2;
//    }else
//    {
//        return 3;
//    }
    CZHero *heroGroup = self.heroGroup [section];
    return heroGroup.heros.count;
}

//给每一行添加数据
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
    
    CZHero *heroGroup = self.heroGroup [indexPath.section];
    cell.textLabel.text = heroGroup.heros [indexPath.row];
    NSLog(@"count=%lu",heroGroup.heros.count);

    
    //设置数据给cell
  /*  NSLog(@"%ld-------%ld",indexPath.section,indexPath.row);
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            cell.imageView.image = [UIImage imageNamed:@"1.jpg"];
            cell.textLabel.text = @"孙悟空";
        }else
        {
            cell.imageView.image = [UIImage imageNamed:@"2.jpg"];
            cell.textLabel.text = @"唐僧";
        }
    }else
    {
        if (indexPath.section == 1) {
            if (indexPath.row == 0) {
                cell.imageView.image = [UIImage imageNamed:@"3.jpg"];
                cell.textLabel.text = @"白骨精";
            }else if(indexPath.row == 1)
            {
                cell.imageView.image = [UIImage imageNamed:@"4.jpg"];
                cell.textLabel.text = @"牛魔王";
            }else
            {
                cell.imageView.image = [UIImage imageNamed:@"5.jpg"];
                cell.textLabel.text = @"山大王";
            }
        }
    }*/
    
    return cell;
}

//4.将hear 和 footer放上去











- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
